package blogwork;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;




/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static String user=null;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
			
	
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		
		//指定参数获取
		String accountStr = request.getParameter("account");
		String passwordStr =request.getParameter("password");
		Map<String, Object>resultMap=new HashMap<>();
		//登录session设置
		HttpSession session = request.getSession();
		session.setAttribute("user", accountStr);
		// 1.建立SQL连接
		Connection conn = null ;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);

			// 2.执行SQL语句

			response.setCharacterEncoding("utf-8");
//			response.setContentType("text/json; charset=UTF-8");
			response.setContentType("application/json;  charset=UTF-8");
			
			String insertSQL = "select * from register where account ='"+accountStr+"' and password='"+passwordStr+"'";
			PreparedStatement pstmt = conn.prepareStatement(insertSQL);
//			pstmt.setString(1, accountStr);
//			pstmt.setString(2, passwordStr);
			ResultSet result = pstmt.executeQuery();

			if(result.next()){
			//if (result.getString("account").equals(accountStr) && result.getString("password").equals(passwordStr)) {			
				resultMap.put("account", result.getString("account"));
				resultMap.put("name", result.getString("name"));
				resultMap.put("avt", result.getString("avt"));
				

				Calendar jcal = new GregorianCalendar();
				jcal.add(Calendar.MONTH, 0);
				jcal.add(Calendar.DAY_OF_MONTH, 0);
				jcal.add(Calendar.HOUR, 0);
				SimpleDateFormat jsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Timestamp login_time=Timestamp.valueOf(jsdf.format(jcal.getTime()));
				String name=result.getString("name");
				String avt=result.getString("avt");
				String insertSQL2 = "insert into user(account,password,name,avt,login_time) values('"+accountStr+"','"+passwordStr+"','"+name+"','"+avt+"','"+login_time+"')";
				pstmt.executeUpdate(insertSQL2);
				response.sendRedirect("/blog_person.html");
				conn.commit();
	
//			}
//			else {	
//				response.getWriter().write("登录失败，账号或密码错误！3秒后自动跳回登录界面……");
//				response.setHeader("Refresh", "3;URL=blog_login.html");
//					}
			}
			else{
						response.getWriter().write("登录失败，账号或密码错误！3秒后自动跳回登录界面……");
						response.setHeader("Refresh", "3;URL=blog_login.html");
					}
		
			result.close();
			pstmt.close();
			conn.close();
		} catch (SQLException sqlE) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		System.out.println(resultMap);	
		PrintWriter out=response.getWriter();
		out.append(JSON.toJSONString(resultMap));
		out.flush();
		out.close();

	}
}
